package com.lagou.controller;

import com.lagou.Hello1Service;
import com.lagou.Hello2Service;
import com.lagou.HelloService;
import com.lagou.filter.TPMonitorFilter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: HelloController
 * @Auther: Jerry
 * @Date: 2020/10/18 22:19
 * @Desctiption: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping
public class HelloController {

    @Reference
    private Hello1Service hello1Service;

    @Reference
    private Hello2Service hello2Service;

    @Reference
    private HelloService helloService;

    @RequestMapping("/sayHello")
    public Map<String, String> sayHello(String name) {
        String result1 = hello1Service.sayHello(name);
        System.out.println(result1);
        String result2 = hello2Service.sayHello(name);
        System.out.println(result2);
        Map<String, String> map = new HashMap<>();
        map.put("result1", result1);
        map.put("result2", result2);
        return map;
    }

    /**
     * 不断调用dubbo服务的3个方法（线程并行调用，确保1分钟内可以被调用2000次以上）
     * @return
     */
    @RequestMapping("/testMonitor")
    public void testMonitor() throws InterruptedException {
        // 线程池中核心线程数的最大值
        int corePoolSize = 100;
        // 线程池中能拥有最多线程数
        int maximumPoolSize = 1000;
        // 表示空闲线程的存活时间
        long keepAliveTime = 5;
        // 用于缓存任务的阻塞队列
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(5000);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
                keepAliveTime, TimeUnit.MILLISECONDS, blockingQueue);
        while (true) {

            Thread.sleep(100);
            executor.execute(()-> {
                helloService.sayHello("Jerry");
                helloService.sayWorld("Jerry");
                helloService.sayGoodbye("Jerry");
            });


            executor.execute(()-> {
                helloService.sayHello("Jerry");
                helloService.sayWorld("Jerry");
                helloService.sayGoodbye("Jerry");
            });

            executor.execute(()-> {
                helloService.sayHello("Jerry");
                helloService.sayWorld("Jerry");
                helloService.sayGoodbye("Jerry");
            });

        }



    }

}
